package com.zhika.zishi.adapter;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.zhika.zishi.R;
import com.zhika.zishi.activity.ProblemDetailsActivity;
import com.zhika.zishi.beans.AnswerOfProblemBean;
import com.zhika.zishi.beans.recommend.RecommendBean;
import com.zhika.zishi.beans.recommend.RecommendProblemBean;
import com.zhika.zishi.beans.recommend.RecommendStrategyBean;
import com.zhika.zishi.util.LogUtil;

import java.util.ArrayList;

/**
 * Created by Michael on 2015/4/8.
 */
public class HomepageAdapter extends BaseAdapter {

    private static final String TAG = "HomepageAdapter";

    private Context context;

    private ArrayList<RecommendBean> homepageList;


    public HomepageAdapter(Context context, ArrayList<RecommendBean> homepageList) {

        this.context = context;

        this.homepageList = homepageList;

    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public int getCount() {
        return homepageList.size();
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        RecommendBean bean = homepageList.get(position);
        String type = bean.getHomepageType();
        if (type.equals("strategy")) {
            View view = LayoutInflater.from(context).inflate(R.layout.item_1_of_homepage, null);
            TextView hobbyText = (TextView) view.findViewById(R.id.item_1_hobby_text);
            TextView strategyText = (TextView) view.findViewById(R.id.item_1_strategy_text);
            TextView contentText = (TextView) view.findViewById(R.id.item_1_content_text);
            ImageView pictureImageView = (ImageView) view.findViewById(R.id.item_1_picture_image_view);
            TextView commentNumText = (TextView) view.findViewById(R.id.item_1_comment_text);
            TextView collectNumText = (TextView) view.findViewById(R.id.item_1_collect_text);
            TextView likeNumText = (TextView) view.findViewById(R.id.item_1_like_text);

            RecommendStrategyBean strategyBean = (RecommendStrategyBean) homepageList.get(position);

            hobbyText.setText(strategyBean.getHobbyStr());
            strategyText.setText(strategyBean.getStrategy());
            contentText.setText(strategyBean.getContentStr());

            /**
             * 现在只是测试
             */
            String pictureUrl = strategyBean.getPictureUrlStr();
            pictureImageView.setImageResource(R.drawable.test_3);

            commentNumText.setText(strategyBean.getCommentNumStr());
            collectNumText.setText(strategyBean.getCollectNumStr());
            likeNumText.setText(strategyBean.getLikeNumStr());

            return view;
        } else {
            View view = LayoutInflater.from(context).inflate(R.layout.item_2_of_homepage, null);
            LinearLayout problemLayout = (LinearLayout) view.findViewById(R.id.item_2_problem_layout);
            problemLayout.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    LogUtil.e(TAG, "问题点击事件");
                    Intent intent = new Intent(context,ProblemDetailsActivity.class);
                    context.startActivity(intent);
                }
            });
            TextView hobbyText = (TextView) view.findViewById(R.id.item_2_hobby_text);
            TextView problemText = (TextView) view.findViewById(R.id.item_2_problem_text);
            TextView commentNumText = (TextView) view.findViewById(R.id.item_2_comment_text);
            TextView collectNumText = (TextView) view.findViewById(R.id.item_2_collect_text);
            ListView listView = (ListView) view.findViewById(R.id.item_2_list_view);
            listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                    Toast.makeText(context,"问题中的答案被点击了",Toast.LENGTH_SHORT).show();
                    LogUtil.e(TAG,"问题中的答案被点击了");
                }
            });

            RecommendProblemBean problemBean = (RecommendProblemBean) homepageList.get(position);
            hobbyText.setText(problemBean.getHobbyStr());
            problemText.setText(problemBean.getProblemStr());
            commentNumText.setText(problemBean.getCommentNumStr());
            collectNumText.setText(problemBean.getCollectNumStr());

            /**
             * 这应该从网络上请求数据
             */
            ArrayList<AnswerOfProblemBean> answerList = new ArrayList<>();
            for (int i = 0; i < 3; i++) {
                AnswerOfProblemBean answerBean = new AnswerOfProblemBean();
                answerBean.setHeaderUrlStr("headerurl");
                answerBean.setNickname("回答者昵称");
                answerBean.setLikeNumStr("333");
                answerBean.setContentStr("回答内容回答内容回答内容回答内容回答内容回答内容");
                answerBean.setPictureUrlStr("pictureUrl");

                answerList.add(answerBean);
            }
            AnswerOfProblemAdapter answerAdapter = new AnswerOfProblemAdapter(context, answerList);
            listView.setAdapter(answerAdapter);

            return view;
        }

    }
}
